<?php

namespace addons\kzvote\controller;

use app\common\model\kzvote\Complaint as ComplaintModel;


class Complaint extends Base
{
    /**
     * 我的投诉
     */
    public function index()
    {
        $user = $this->auth->getUser();
        $model = new ComplaintModel;
        $list = $model
            ->where('user_id', $user['id'])
            ->order('id desc')
            ->paginate(20, true);
        $this->success('请求成功', $list);
    }

    public function detail()
    {
        $id = $this->request->param('id',0);
        $detail = ComplaintModel::get($id);
        if (!$detail) {
            $this->error('投诉不存在');
        }
        $this->success('请求成功', $detail);
    }

    public function del()
    {
        $id = $this->request->param('id',0);
        $user = $this->auth->getUser();
        $detail = ComplaintModel::get($id);
        if (!$detail) {
            $this->error('投诉不存在');
        }
        if ($user['id'] != $detail['user_id']) {
            $this->error('无权删除');
        }
        if ($detail->delete()) {
            $this->success('请求成功');
        }
        $this->error('删除失败');
    }

    public function save()
    {
        $user = $this->auth->getUser();
        $post = $this->request->post();
        if (isset($post['id']) && $post['id']) {
            $model = ComplaintModel::get($post['id']);
        } else {
            $model = new ComplaintModel;
        }
        $post['user_id'] = $user['id'];
        if ($model->allowField(true)->save($post)) {
            $this->success('投诉成功');
        }
        $this->error('投诉失败');
    }

}